import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import config from "./settings";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"; //svg
// import viteCompression from "vite-plugin-compression"; // 静态资源压缩
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { visualizer } from "rollup-plugin-visualizer";

export default defineConfig({
  plugins: [
    vue(),
    visualizer({ open: true }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/SvgIcon")],
      symbolId: "[name]"
    }),
    AutoImport({
      // 需要去解析的文件
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/,
        /\.vue\?vue/, // .vue
        /\.md$/ // .md
      ],
      imports: ["vue", "vue-router"],
      eslintrc: {
        enabled: false,
        filepath: "./.eslintrc-auto-import.json",
        globalsPropValue: true
      },
      resolvers: [ElementPlusResolver()],
      injectAtEnd: false
    }),
    Components({
      resolvers: [ElementPlusResolver({ importStyle: false })]
    })
    // viteCompression({
    //   threshold: 512000,
    //   deleteOriginFile: true
    // }),
  ],
  ssr: true, // 服务端渲染
  base: "./", // 在生产中服务时的基本公共路径
  outDir: "dist", // 构建输出将放在其中。如果目录存在，它将在构建之前被删除
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
      "@components": path.resolve(__dirname, "src/components"),
      "@views": path.resolve(__dirname, "src/views"),
      "@assets": path.resolve(__dirname, "src/assets")
    }
  },
  // 反向代理
  server: {
    host: "localhost", // 指定服务器主机名
    port: 8080, // 指定服务器端口
    base: "./",
    hmr: true, // 开启热更新
    open: true, // 在服务器启动时自动在浏览器中打开应用程序
    strictPort: false, // 设为 false 时，若端口已被占用则会尝试下一个可用端口,而不是直接退出
    https: false, // 是否开启 https
    cors: true, // 为开发服务器配置 CORS。默认启用并允许任何源
    proxy: {
      "/api": {
        target: config.request_url,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "")
      },
      "/geojson": {
        target: "https://geo.datav.aliyun.com/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/geojson/, "")
      },
      "/datas": {
        target: "https://lab.isaaclin.cn/nCoV/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/datas/, "")
      },
      "/apinews": {
        target: "https://c.m.163.com/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/apinews/, "")
      }
    }
  },
  // 打包配置
  build: {
    outDir: "dist",
    assetsDir: "static",
    chunkSizeWarningLimit: 1000,
    minify: "terser",
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    rollupOptions: {
      input: {
        index: path.resolve(__dirname, "index.html")
      },
      output: {
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
        assetFileNames: "static/[ext]/name-[hash].[ext]"
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@use "@/assets/global.scss" as *;'
      }
    }
  }
});
